﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomerOrderingSystem

{
    public partial class CustomerInfo : Form
    {
        private string sql = "";
        public CustomerInfo()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        private void CustomerInfo_Load(object sender, EventArgs e)
        {
            clientDataGridView.DataSource = sql_cus.QueryDataTable0();
        }
        private void MakeSqlStr()
        {
            {
                //清空上次的查询字符串
                sql = "";
                if (cnoTextBox.Text.Trim() != string.Empty)
                {
                    sql = " and Cno like '%" + cnoTextBox.Text.Trim() + "%'";
                }
                if (cnameTextBox.Text.Trim() != string.Empty)
                {
                    sql += " and Cname like '%" + cnameTextBox.Text.Trim() + "%'";
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MakeSqlStr();
            clientDataGridView.DataSource = sql_cus.QueryDataTable(sql);
        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int cnt = sql_cus.QueryDataTable1(cnoTextBox1.Text);
                if (cnt == 1)
                {
                    MessageBox.Show("更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sql_cus.QueryDataTable2(cnameTextBox1.Text, caddrTextBox.Text, ctelTextBox.Text, cnoTextBox1.Text);
                }
                // 添加新记录
                else
                {
                    MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK);
                    sql_cus.QueryDataTable3(cnoTextBox1.Text, cnameTextBox1.Text, caddrTextBox.Text, ctelTextBox.Text);
                }
                CustomerInfo_Load(null, null);
            }
            finally
            {
                sql_cus.QueryDataTable4();
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult ret = MessageBox.Show("确定要删除记录吗？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ret == DialogResult.Cancel)
            {
                return;
            }

            try
            {

                if (sql_cus.QueryDataTable5(cnoTextBox1.Text) == 1)
                {
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                CustomerInfo_Load(null, null);
            }
            finally
            {
                sql_cus.QueryDataTable4();
            }

        }
    }
}
